$(function(){
	var basePath=$("#basepath").val();
	$("#unchooseListFilter_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#unchooseListFilter_form select[name='department']");
		$depSelect.html("");
		var $classSelect=$("#unchooseListFilter_form select[name='classInfo']");
		$classSelect.html("");
		var orgDepId=$depSelect.attr("data-org");
		$depSelect.attr("data-org","0");
		var $goBtn=$("#unchooseListFilter_go");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var option= $(" <option/>").val(data[i].id).html(data[i].name);
							if(data[i].id==orgDepId){
								option.prop("selected",true);
							}
							option.appendTo($depSelect)
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($depSelect);
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							$goBtn.attr("disabled",true); 
						}else{
							$goBtn.attr("disabled",false); 
							if(orgDepId!=0){
								$depSelect.val(orgDepId);
							}
							$depSelect.change();
						}
					}
			}, "json");
	});
	$("#unchooseListFilter_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $classSelect=$("#unchooseListFilter_form select[name='classInfo']");
		$classSelect.html("");
		var orgClassId=$classSelect.attr("data-org");
		$classSelect.attr("data-org","0");
		var $goBtn=$("#unchooseListFilter_go");
		$.get(basePath+ "admin/class/getClassesByDep/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var option= $(" <option/>").val(data[i].id).html(data[i].name);
							if(data[i].id==orgClassId){
								option.prop("selected",true);
							}
							option.appendTo($classSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							$goBtn.attr("disabled",true); 
						}else{
							$goBtn.attr("disabled",false); 
						}
					}
			}, "json");
	});

	$("#unchooseListFilter_form select[name='college']").change();
	$("#unchooseListFilter_go").click(function(){
		var classId=$("#unchooseListFilter_form select[name='classInfo']").val();
		if(classId==0){
			return;
		}
		window.location=basePath+"admin/choose/showUnchooseList/"+classId+"/1";
	});
	$(".chooseManual").click(function(){
		var stuId=$(this).attr("data-studentId");
		$("#choose_manual_box input[name=stuId]").val(stuId);
		$("#choose_manual_box_alert_container").html("");
		$("#choose_manual_box").modal("show");
		$("#choose_manual_box .ajax_wait").show();
		$("#project_list_choose").hide();
		$("#project_list_choose").css("height","50px");
		$("#project_list_choose_detail").html("请选择题目");
		var $prjSelect=$("#project_list_choose select[name=project_select]").html("<option value='0'></option>");
		$prjSelect.chosen({no_results_text: "没有对应题目:",search_contains:true,width: "95%"}); 
		$.get(basePath+ "admin/choose/getAvailableProjectOfStudent/"+stuId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var option= $(" <option/>").val(data[i].id).html(data[i].name);
							option.appendTo($prjSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无可用题目</option>").appendTo($prjSelect);
							makeAlert("无可用题目,请添加或审核之后再试","error",$("#choose_manual_box_alert_container"),"no_prj_error");
						}else{
							$("#choose_manual_box_alert_container .no_prj_error").alert("close");
						}

						$prjSelect.trigger('chosen:updated');//重新生成列表
						$("#choose_manual_box .ajax_wait").hide();
						$("#project_list_choose").show();
					}
			}, "json");
	});
	$('#project_list_choose select[name=project_select]').on('chosen:showing_dropdown', function(evt, params) {
		var drop=params.chosen.dropdown;
		var height=50;
		height+=parseInt(drop.css("height"));
		var orgHeight=parseInt($("#project_list_choose").css("height"))
		if(orgHeight<height){
			$("#project_list_choose").css("height",height+"px");
		}
	});
	$('#project_list_choose select[name=project_select]').on('chosen:hiding_dropdown', function(evt, params) {
		var isSelected=$(this).val()!=0;
		if(!isSelected){
			$("#project_list_choose").css("height","50px");
		}else{
			var height=50+parseInt($("#project_list_choose_detail").css("height"));
			$("#project_list_choose").css("height",height+"px");
		}
	});
	$('#project_list_choose select[name=project_select]').change(function(){
		var prjId=$(this).val();
		$.get(basePath+ "admin/project/getProjectByPrjId/"+prjId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						if(data.length==0){
							alert("无此题目,请重试");
							history.go(0);
						}
						var detail="";
						detail+="题目名称:"+data.name+"<br/>"
						detail+="指导教师:"+data.teacher.name+"<br/>";
						detail+="评阅人:"+data.shengyue_teacher.name+"<br/>"
						detail+="描述:"+data.description;
						$("#project_list_choose_detail").html(detail);
						var height=50+parseInt($("#project_list_choose_detail").css("height"));
						$("#project_list_choose").css("height",height+"px");
					}
			}, "json");
	});
	$("#choose_manual_box_confirm").click(function(){
		var prjId=$('#project_list_choose select[name=project_select]').val();
		if(prjId==0){
			makeAlert("请选择题目","error",$("#choose_manual_box_alert_container"),"no_prj_error");
			return;
		}
		var stuId=$("#choose_manual_box input[name=stuId]").val();
		if(stuId==0){
			alert("未选择学生,请重试");
			return;
		}
		$("#choose_manual_box_alert_container .no_prj_error").alert("close");
		$.get(basePath+ "admin/choose/setChooseManual/"+prjId+"/"+stuId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							alert("请选择题目");
							break;
						case "-3":
							alert("请选择学生");
							break;
						case "-4":
							alert("此题目已经被其他学生选择");
							history.go(0);
							break;
						case "-5":
							alert("此题目未经审核通过，请先审核");
							break;
						case "-6":
							alert("此题目适用专业方向不能用于此学生");
							break;
						case "-7":
							alert("此学生已经选择其他题目");
							history.go(0);
							break;
						case "-8":
							alert("出现错误,无法完成操作");
							history.go(0);
							break;
						case "0":
							noty({text:"选择成功,正在刷新"});
							history.go(0);
							break;
						}
					}
				},"text");
	});
});